﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using devicesDll;
using System.Net;
using System.IO;
using Database;
using ControlService;


namespace testConsoleProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            bool writeEnd = true;

            //Database Test

            /*DBConnect db = new DBConnect(args[0], args[1], args[2], args[3]);
            db.OpenConnection();
            Configuration cf = db.GetAllConfiguration();
            Console.WriteLine(cf.IPDHCP);
            Console.WriteLine(cf.IPNetDHCP);
            Console.WriteLine(cf.MaxThreads);
            Console.WriteLine(cf.ScanTime);
            Console.WriteLine(cf.Timeout);
            /*List<SwitchDB> s = db.GetAllSwitch();
            Console.WriteLine(s.Count);
            foreach (SwitchDB item in s)
            {
                Console.WriteLine(item.Ip);
                Console.WriteLine(item.Name);
                Console.WriteLine(item.Id);
            }
            db.CloseConnection();*/

            //Switch Test

            /*Console.WriteLine("Test - Switch");
            Switch a = new Switch(IPAddress.Parse("10.144.0.65"), 2);
            try
            {
                a.Query(true, "default");
                a.Timeout = 4000;
                for (int i = 0; i < a.Mac.Length; i++)
			{
                Console.WriteLine(a.Port[i] +"  "+ a.Mac[i]);
			}
                
            }
            catch (Exception)
            {
                throw;
            }*/

            //Queue Test

            /*d.Queue.AddQueryRange(new object[][] {  new object[] { new Device(), false, "hallo" },
                                                    new object[] { new Device(), false, "tschuess"}});*/

            //Device Dll Full Test

            /*Console.WriteLine("---------deviceDLL Test-------------");
            IpVlan[] ips = new IpVlan[] { new IpVlan(IPAddress.Parse("10.144.0.65"),2) };
            int timeout = 10000;
            //int[] vlan = new int[]{2};
            IPAddress[] dhcpNet = new IPAddress[]{IPAddress.Parse("10.144.0.0")};
            IPAddress dhcpServer = IPAddress.Parse("10.144.0.2");
            Devices test= new Devices(ips, timeout, dhcpNet,dhcpServer);
            test.WriteProgressMethod = new WriteProgress(WriteProgress);

            test.FullScan();

            StreamWriter file = new StreamWriter(@"test.txt");
            foreach (Device item in test)
            {
                if (item.GetType() == typeof(Pc))
                {
                    Pc pcItem = (Pc)item;
                    file.WriteLine("PC");
                    file.WriteLine(item.IP);
                    file.WriteLine(item.Mac[0]);
                    file.WriteLine(item.Port[0]);
                    file.WriteLine("OS: "+pcItem.OS);
                    file.WriteLine("RAM in MB: " + pcItem.RAM);
                    file.WriteLine("Connectionfailed:" + item.ConnectionFailed.ToString());
                }
                if(item.GetType() == typeof(Switch))
                {
                    file.WriteLine("Switch");
                    file.WriteLine(item.IP);
                    file.WriteLine(item.Mac[0]);
                    file.WriteLine(item.Port[0]);
                }
                if (item.GetType() == typeof(Device))
                {
                    file.WriteLine("Irgendwas");
                    file.WriteLine(item.IP);
                    file.WriteLine(item.Mac[0]);
                    file.WriteLine(item.Port[0]);
                }
                if (item.GetType() == typeof(Printer))
                {
                    file.WriteLine("fake Drucker");
                    file.WriteLine(item.IP);
                    file.WriteLine(item.Mac[0]);
                    file.WriteLine(item.Port[0]);
                }
            }
           
            file.Close();   */

            //Full System Test

            /*ControlService.ControlService cs = new ControlService.ControlService();
            cs.Initialize();
            Console.WriteLine("Started");
            Console.WriteLine("Press any Key to continue");
            Console.ReadKey();
            cs.ExecuteFullScan();*/

            //Identify Devices Test

            /* writeEnd=false;
            IdentifyDevices id = new IdentifyDevices();
            List<DeviceSignature> sig = new List<DeviceSignature>();;
            sig.AddRange(new DeviceSignature[] {new DeviceSignature("10.144.0.6","testmac1","testport1"),
                                                new DeviceSignature("10.144.0.7","testmac2","testport2"),
                                                new DeviceSignature("10.144.0.8","testmac3","testport3"),
                                                new DeviceSignature("10.144.0.9","testmac4","testport4"),});
            id.StartAsyncIdentify(sig,2,161,1000);
            id.IdentifyDevicesFinished+=new AsyncIdentifyDevicesFinishedEventHandler(id_IdentifyDevicesFinished);*/


            //Long Term Test

            //uncoment class ltTest
            /*writeEnd = false;
            ltTest test = new ltTest();
            test.StartTest();
            Console.WriteLine("Press any key to stop after active Scan");
            Console.ReadKey();
            test.Stop();*/

            if (writeEnd)
                Console.WriteLine("\n\n End");
            Console.ReadKey();
        }

        static void id_IdentifyDevicesFinished(object sender, IdentifyDevicesEventArgs e)
        {
            foreach (DeviceSignature item in e.Devices)
            {
                Console.WriteLine(item.ip + " " + item.mac + " " + item.port + " " + item.type.ToString());
            }
            Console.WriteLine("END");
        }


        static void WriteProgress(QueryProgress progress)
        {
            Console.WriteLine(progress);
        }

        /*class ltTest
        {
            volatile bool should_stop = false;
            public void Stop()
            {
                should_stop = true;
            }

            public void StartTest()
            {
                System.Threading.Thread th = new System.Threading.Thread(ExecMethod);
                th.Start();
            }

            private void ExecMethod()
            {
                ControlService.ControlService cs = new ControlService.ControlService();
                cs.Initialize();
                while (!should_stop)
                    cs.ExecuteFullScan();

                Console.WriteLine("Scan finished");
                Console.WriteLine("Program can be exited");
            }
        }*/
    }
}
